﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace ChatBox.Web.UI
{
    public partial class Chat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

                if (Session["user"] != null || Session["iduser"] != null)
                {
                    BindingRepeater();
                }
                else
                {
                    Response.Redirect("index.aspx");
                }
            }
            lblChao.Text = "<span style = 'color:white;'>" + Session["user"] + "</span>";
        }

        public void BindingRepeater()
        {
            string sql = "select * from TaiKhoan where TaiKhoan != '"+Session["user"]+"'";
            DataSet dulieu = LopKetNoi.TruyVanDuLieu(sql,"tk");
            rptTaiKhoan.DataSource = dulieu.Tables["tk"];
            rptTaiKhoan.DataBind();
        }

        [WebMethod]
        public static void Send(string msg,string pNhan)
        {
            string pGui = HttpContext.Current.Session["iduser"].ToString();
            string sqlKiemTraTonTai = "select * from TinNhan where NguoiNhan = " + pNhan + " and NguoiGui = " + pGui;
            DataSet dulieu =  LopKetNoi.TruyVanDuLieu(sqlKiemTraTonTai,"kt");

            string sqlTenNguoiGui = "select * from taikhoan where idtaikhoan =" + pGui;
            DataSet dlTenNguoiGui = LopKetNoi.TruyVanDuLieu(sqlTenNguoiGui,"tng");

            string sqlTenNguoiNhan = "select * from taikhoan where idtaikhoan =" + pGui;
            DataSet dlTenNguoiNhan = LopKetNoi.TruyVanDuLieu(sqlTenNguoiNhan, "tnn");

            string sql = "";
            string noidungdi = "<p><span class = mauxanh>" + dlTenNguoiGui.Tables["tng"].Rows[0][1] + ":</span>" + msg + "</p>";
            string noidungnhan = "<p><span class = maudo>" + dlTenNguoiNhan.Tables["tnn"].Rows[0][1] + ":</span>" + msg + "</p>";

            if (dulieu.Tables["kt"].Rows.Count > 0)
            {
                sql = "Update TinNhan set NoiDung ='" + noidungdi + "' where NguoiNhan = " + pNhan + " and NguoiGui = " + pGui;
                LopKetNoi.CapNhatDuLieu(sql);

                sql = "Update TinNhan set NoiDung ='" + noidungnhan + "' where NguoiNhan = " + pGui + " and NguoiGui = " + pNhan;
                LopKetNoi.CapNhatDuLieu(sql);
            }
            else
            {
                sql = "Insert into TinNhan (NguoiNhan,NguoiGui,NoiDung) values (" + pNhan + "," + pGui + ",'" + noidungdi +
                      "')";
                LopKetNoi.CapNhatDuLieu(sql);

                sql = "Insert into TinNhan (NguoiNhan,NguoiGui,NoiDung) values (" + pGui + "," + pNhan + ",'" + noidungnhan +
                      "')";
                LopKetNoi.CapNhatDuLieu(sql);
            }
        }

        [WebMethod]
        public static string Receive(string pNhan)
        {
            string pGui = HttpContext.Current.Session["iduser"].ToString();
            string sql = "select * from TinNhan where NguoiNhan = " + pNhan + " and NguoiGui = " + pGui ;
            DataSet dulieu = LopKetNoi.TruyVanDuLieu(sql,"nd");

            string a = "";

                sql = "Update TinNhan set NoiDung =' ' where NguoiNhan = " + pNhan + " and NguoiGui = " + pGui;
                LopKetNoi.CapNhatDuLieu(sql);


                if (dulieu.Tables["nd"].Rows.Count > 0)
                {
                    a = dulieu.Tables["nd"].Rows[0][3].ToString();

                }
            return a;
        }

        //[WebMethod]
        //public static string TinNhan()
        //{
        //    string strTinNhan = "";
        //    string sqlNguoiNhan = "select * from tinnhan where nguoinhan = " + HttpContext.Current.Session["iduser"];

        //    DataSet duLieuNguoiNhan = LopKetNoi.TruyVanDuLieu(sqlNguoiNhan, "nn");
        //    for (int i = 0; i < duLieuNguoiNhan.Tables["nn"].Rows.Count; i++)
        //    {
        //        string strNoiDungNN = duLieuNguoiNhan.Tables["nn"].Rows[i][3].ToString();
        //        if (strNoiDungNN != "")
        //        {
        //            string sqlTaiKhoan = "select * from taikhoan where idtaikhoan = " +
        //                                 duLieuNguoiNhan.Tables["nn"].Rows[i][1];

        //            DataSet duLieuTK = LopKetNoi.TruyVanDuLieu(sqlTaiKhoan, "taikhoan");

        //            strTinNhan += duLieuTK.Tables["taikhoan"].Rows[0][0] + " " +
        //                          duLieuTK.Tables["taikhoan"].Rows[0][1] + ",";
        //        }
        //    }
        //    return strTinNhan;
        //}


        protected void lbnThoat_Click(object sender, EventArgs e)
        {
            Session["user"] = null;
            Session["iduser"] = null;
            Response.Redirect("index.aspx");
        }
    }
}